package com.project.nimsen.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.project.jdbc.dao.IBaseJdbcDao;
import com.project.jdbc.service.BaseServiceImpl;
import com.project.jdbc.utils.Finder;
import com.project.nimsen.dao.IBaseDao;
import com.project.nimsen.dto.MemberDto;
import com.project.nimsen.entity.TMerberEntity;
import com.project.nimsen.entity.TprofitEntity;
import com.project.nimsen.service.IMemberService;
@Repository("memberService")
public class MemberServiceImpl extends BaseServiceImpl implements
		IMemberService {
	@Resource
	IBaseDao baseDao;
	protected IBaseJdbcDao getBaseDao() {
		return baseDao;
	}
	public List<Map<String, Object>> findByDto(MemberDto dto) throws Exception {
		Finder finder = new Finder("select * from T_MEMBER where active!=1");
		if(StringUtils.isNotBlank(dto.getActive())){
			finder.append(" and active=:active");
			finder.setParam("active", dto.getActive());
		}
		if(StringUtils.isNotBlank(dto.getPid())){
			finder.append(" and PID=:pid");
			finder.setParam("pid", dto.getPid());
		}
		finder.append(" order by CREATE_TIME desc");
		return baseDao.queryForList(finder, dto.getPage());
	}
	//验证是否是修改实体
	private void addIdInfo(TMerberEntity ent, Finder finder)throws Exception{
		if(StringUtils.isNotBlank(ent.getID())){
			finder.append(" and id!=:id");
			finder.setParam("id", ent.getID());
		}
	}
	//验证证属性是否重复
	private int valpro(TMerberEntity ent, String pro)throws Exception{
		Finder finder = new Finder("select count(id) from T_MEMBER where active!=1");
		finder.append(" and ");
		finder.append(pro);
		finder.append("=:");
		finder.append(pro);
		if("mphone".equals(pro))
			finder.setParam(pro, ent.getMPHONE());
		else if ("card_id".equals(pro))
			finder.setParam(pro, ent.getCARD_ID());
		else if("bank_card_id".equals(pro))
			finder.setParam(pro, ent.getBANK_CARD_ID());
		else
			finder.setParam(pro, ent.getWEIXIN());
		addIdInfo(ent, finder);
		return baseDao.queryForObject(finder, Integer.class);
	}
	public String findVal(TMerberEntity ent) throws Exception {
		//验证后机号
		if(valpro(ent,"mphone")>0){
			return "手机号已经在系统里存在";
		}
		//验证身份证号
		if(valpro(ent,"card_id")>0){
			return "身份证号已经在系统里存在";
		}
		if(valpro(ent,"bank_card_id")>0){
			return "银行卡号已经在系统里存在";
		}
		if(valpro(ent,"weixin")>0){
			return "微信号已经在系统里存在";
		}
		return "true";
	}
	public boolean saveEnt(TMerberEntity ent, MemberDto dto) throws Exception {
		String pid = ent.getPID();
		ent.setCODES(findNextCode(pid));
		Object id = baseDao.saveEntity(ent);
		if(id==null)
			return false;
		//如果是用户直接添加的就直接生成反利信息
		if((ent.getACTIVE()-2)!=0||StringUtils.isBlank(ent.getPID())){
			return true;
		}
		return init_profit(ent, dto, id.toString());
	}
	private boolean init_profit(TMerberEntity ent, MemberDto dto, String s_userid)throws Exception{
		Date now = new Date();
		dto.setNow_time(now);
		//第一层反利信息
		TprofitEntity first = initPro(ent.getPID(),s_userid, dto, "1");
		if(baseDao.saveEntity(first)==null){
			return false;
		}
		//第二层反利信息
		String sec_pid = finderPid(ent.getPID());
		if(StringUtils.isBlank(sec_pid))
			return true;
		TprofitEntity sec = initPro(sec_pid,s_userid, dto, "2");
		if(baseDao.saveEntity(sec)==null){
			return false;
		}
		//第三层反利信息
		String thi_pid=finderPid(sec_pid);
		if(StringUtils.isBlank(thi_pid))
			return true;
		TprofitEntity thi = initPro(thi_pid,s_userid, dto, "3");
		if(baseDao.saveEntity(thi)==null){
			return false;
		}
		return true;
	}
	//初始化反利信息
	private TprofitEntity initPro(String pid, String s_userid, MemberDto dto, String type){
		TprofitEntity first = new TprofitEntity();
		first.setS_USER_ID(s_userid);
		first.setP_USER_ID(pid);
		if("1".equals(type)){
			first.setGRADE(1);
			first.setMONEYS(150);
		}else if("2".equals(type)){
			first.setMONEYS(90);
		}else{
			first.setMONEYS(60);
		}
		first.setGRADE(Integer.parseInt(type));
		first.setBACK_INFO(1);
		first.setUSER_ID_S(dto.getLogin_user_id());
		first.setCREATE_TIME(dto.getNow_time());
		return first;
	}
	//得到父级编号
	private String finderPid(String id)throws Exception{
		Finder finder = new Finder("select pid from T_MEMBER where id=:id");
		finder.setParam("id", id);
		Object pid = baseDao.queryForObject(finder, Object.class);
		if(pid==null||StringUtils.isBlank(pid.toString())){
			return null;
		}
		return pid.toString();
	}
	public TMerberEntity findByID(String id) throws Exception {
		if(StringUtils.isBlank(id)){
			return null;
		}
		Finder finder = new Finder("select * from T_MEMBER where id=:id");
		finder.setParam("id", id);
		return baseDao.queryForObject(finder, TMerberEntity.class);
	}
	public boolean updateSKByIds(String ids, String cur_userid) throws Exception {
		String [] arr_str = ids.split(",");
		List<String> list_strs = new ArrayList<String>();
		for(String str : arr_str){
			list_strs.add(str);
		}
		Date now = new Date();
		Finder finder = new Finder("update T_MEMBER set ACTIVE=2, EXA_TIME=:now where id in ( :idsw )");
		finder.setParam("now", now);
		finder.setParam("idsw", list_strs);
		if(baseDao.update(finder)<1)
			return false;
		MemberDto dto = new MemberDto();
		dto.setLogin_user_id(cur_userid);
		for(String id : list_strs){
			TMerberEntity ent = findByID(id);
			if(!init_profit(ent, dto, id))
				return false;
		}
		return true;
	}
	public String findNextCode(String pid) throws Exception {
		Finder finder = new Finder("select count(id) from T_MEMBER where ");
		if(StringUtils.isNotBlank(pid)){
			finder.append("pid=:pid");
			finder.setParam("pid", pid);
		}else{
			finder.append("pid is null");
		}
		Integer count = baseDao.queryForObject(finder, Integer.class);
		count++;
		//当为顶级的时候
		if(StringUtils.isBlank(pid)){
			return count+"";
		}
		Finder p_finder = new Finder("select codes from T_MEMBER where id=:pid");
		p_finder.setParam("pid", pid);
		String pcodes = baseDao.queryForObject(p_finder, String.class);
		return pcodes+"-"+count;
	}
}
